package com.itboye.jinpuyiyuan.activity.home.healthconsultation;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.itboye.jinpuyiyuan.R;
import com.itboye.jinpuyiyuan.adapter.HealthConsultationAdapter;
import com.itboye.jinpuyiyuan.base.BaseActivity;
import com.itboye.jinpuyiyuan.bean.ArticleListBean;
import com.itboye.jinpuyiyuan.presenter.BranchAddressPresenter;
import com.itboye.jinpuyiyuan.utils.volley.ResultEntity;
import com.scwang.smartrefresh.header.MaterialHeader;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.DefaultRefreshFooterCreater;
import com.scwang.smartrefresh.layout.api.DefaultRefreshHeaderCreater;
import com.scwang.smartrefresh.layout.api.RefreshFooter;
import com.scwang.smartrefresh.layout.api.RefreshHeader;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.constant.SpinnerStyle;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;
import java.util.Observable;
import java.util.Observer;

/**
 * Created by Administrator on 2017/12/11 0011.
 * 健康资讯
 */

public class HealthConsultationActivity extends BaseActivity implements Observer {
    //title
    private ImageView leftImg,rightImg;
    private TextView leftTv,tvTitle;
    private RelativeLayout rlLeft,toobar;

    private SmartRefreshLayout refresh;
    private ListView healthConsultationList;
    private HealthConsultationAdapter healthConsultationAdapter;
    private ArrayList<ArticleListBean> list;

    private BranchAddressPresenter presenter;
    private int pageNo=1;
    @Override
    protected void onCreate(Bundle arg0) {
        super.onCreate(arg0);
        setContentView(R.layout.activity_healthconsultation);
        initViewData();
        list=new ArrayList<>();

        presenter=new BranchAddressPresenter(this);
        caozuo();

        healthConsultationAdapter=new HealthConsultationAdapter(this,list);
        healthConsultationList.setAdapter(healthConsultationAdapter);
        healthConsultationList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(HealthConsultationActivity.this, HealthConsultationDetailActivity.class);
                intent.putExtra("articleId",list.get(position).getId());
                intent.putExtra("islike",list.get(position).getIs_like());
                startActivity(intent);
            }
        });
    }

    private void initViewData() {
        leftTv.setVisibility(View.GONE);
        tvTitle.setText("健康资讯");
        rightImg.setVisibility(View.GONE);

    }

    private void caozuo(){
        refresh = (SmartRefreshLayout) findViewById(R.id.refresh);
        refresh.setEnableAutoLoadmore(true);//开启自动加载功能（非必须）

        //        //触发自动刷新
        refresh.autoRefresh();
        refresh.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                refreshlayout.getLayout().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        pageNo = 1;
//                        00P003开头的(001=>帮助中心,002=>用户协议,003=>公告,004=>金普资讯)
                        presenter.queryArticle("00P003004","","published",pageNo,10);//查询文章
                    }
                }, 0);
            }
        });

        //进行加载更多的逻辑处理
        refresh.setOnLoadmoreListener(new OnLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                refreshlayout.getLayout().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        pageNo++;
                        presenter.queryArticle("","","published",pageNo,10);//查询文章
                    }
                }, 0);
            }
        });

        //设置头布局样式,全局有效
        refresh.setDefaultRefreshHeaderCreater(new DefaultRefreshHeaderCreater() {
            @Override
            public RefreshHeader createRefreshHeader(Context context, RefreshLayout layout) {
                //全局设置主题颜色
                layout.setPrimaryColorsId(R.color.colorPrimary, android.R.color.white);
                //指定为经典Header，默认是 贝塞尔雷达Header
                return new ClassicsHeader(context).setSpinnerStyle(SpinnerStyle.Translate);
            }
        });
        //设置脚布局样式,全局有效
        refresh.setDefaultRefreshFooterCreater(new DefaultRefreshFooterCreater() {
            @Override
            public RefreshFooter createRefreshFooter(Context context, RefreshLayout layout) {
                //指定为经典Footer，默认是 BallPulseFooter
                return new ClassicsFooter(context).setSpinnerStyle(SpinnerStyle.Translate);
            }
        });
        refresh.setRefreshHeader(new MaterialHeader(this).setShowBezierWave(true));
        //  refresh.setRefreshFooter(new BallPulseFooter(this).setSpinnerStyle(SpinnerStyle.Scale));
        refresh.finishLoadmore();
    }
    @Override
    public void onClick(View view) {
        super.onClick(view);
        switch (view.getId()){
            case R.id.rlLeft:
            case R.id.leftImg:
                this.finish();
                break;

        }
    }


    @Override
    public void update(Observable o, Object arg) {
        ResultEntity resultEntity = handlerError(arg);
        if (resultEntity!=null){
            if (resultEntity.getEventType()== BranchAddressPresenter.By_Post_queryByCateOrTag_success){
              List<ArticleListBean> listBeans= (List<ArticleListBean>) resultEntity.getData();
                if (pageNo == 1) {//下拉刷新
                    list.clear();
                    healthConsultationAdapter.notifyDataSetChanged();
                    //结束加载
                    refresh.finishRefresh();
                } else {//上拉加载
                    healthConsultationAdapter.notifyDataSetChanged();
                    //结束加载
                    refresh.finishLoadmore();
                }
              list.addAll(listBeans);
              healthConsultationAdapter.addData(list);
            }else if (resultEntity.getEventType()== BranchAddressPresenter.By_Post_queryByCateOrTag_fail){

            }
        }
    }
}
